//
//  InputBar.swift
//  Game
//
//  Created by woniu on 2025/8/26.
//

import Foundation
import UIKit

class InputBar: UIView {
    public var onPickerImgBlock: ((_ index:Int)->())?
    
    public var indexLab:UILabel!
    public var inputField:UITextField!
    public var imgView:UIImageView!
    public var deleButton:UIButton!
    private var wordItem = WordItem()
    private var linView:UIView!
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        indexLab = UILabel()
        indexLab.font = UIFont.boldSystemFont(ofSize: 13)
        indexLab.textColor = Colors.title
        indexLab.textAlignment = .center
        indexLab.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        self.addSubview(indexLab)
        
        inputField = UITextField()
        inputField.textColor = .black
        inputField.placeholder = "输入单词"
        inputField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 44))
        inputField.leftViewMode = .always
        inputField.clearButtonMode = .always
        inputField.font = UIFont.boldSystemFont(ofSize: 16)
        inputField.borderSet(Colors.line2, 1)
        inputField.circular5()
        self.addSubview(inputField)
        
        imgView = UIImageView()
        imgView.image = UIImage(named: "添加图片")?.withTintColor(Colors.title3)
        imgView.isUserInteractionEnabled = true
        imgView.contentMode = .scaleAspectFit
        let tap = UITapGestureRecognizer(target: self, action: #selector(onTap(_:)))
        self.addGestureRecognizer(tap)
        self.addSubview(imgView)
        
        deleButton = UIButton(type:.custom)
        deleButton.setTitle("清除", for: .normal)
        deleButton.setTitleColor(.red, for: .normal)
        deleButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        deleButton.addTarget(self, action: #selector(onRemove), for: .touchUpInside)
        self.addSubview(deleButton)
        
        linView = UIView()
        linView.backgroundColor = Colors.line2
        self.addSubview(linView)
    }
    
    public func update(wordItem:WordItem){
        self.wordItem = wordItem
        inputField.text = wordItem.word
        imgView.image = wordItem.image ?? UIImage(named: "添加图片")?.withTintColor(Colors.title3)
    }
    
    public func didSelected(icon:UIImage?){
        //删除本地图片
        if !wordItem.imgName.isEmpty{
            GameManager.share.delete(imgNames: [wordItem.imgName])
        }
        self.wordItem.image = icon
        self.wordItem.didModifyImg = true
        imgView.image = icon ?? UIImage(named: "添加图片")?.withTintColor(Colors.title3)
    }
    
    public func getWordItem()->WordItem?{
        var word = inputField.text ?? ""
        word = word.replaceRN()
        //图片和文字并存
        if word.isEmpty{
            return nil
        }
        if wordItem.image == nil{
            return nil
        }
        self.wordItem.word = word
        return self.wordItem
    }
    
    @objc func onRemove(){
        //删除本地图片
        if !wordItem.imgName.isEmpty{
            GameManager.share.delete(imgNames: [wordItem.imgName])
        }
        self.wordItem.imgName = ""
        self.wordItem.image = nil
        self.wordItem.word = ""
        self.wordItem.didModifyImg = true
        self.update(wordItem: self.wordItem)
    }
    
    @objc func onTap(_ deco:UITapGestureRecognizer){
        if let icon = self.wordItem.image{
            let sheet = UIAlertController(
                title: "请选择",
                message: nil,
                preferredStyle: .actionSheet
            )
           
            sheet.addAction(UIAlertAction(title: "查看大图", style: .default) {_ in
                PhotoBrowserService().show(0, [icon])
            })
            sheet.addAction(UIAlertAction(title: "删除", style: .default) {[weak self] _ in
                guard let `self` = self else { return }
                self.didSelected(icon: nil)
            })
            sheet.addAction(UIAlertAction(title: "取消", style: .cancel){_ in
            })
            MyNavigator.getTopVC()?.present(sheet, animated: true)
        }else{
            self.onPickerImgBlock?(self.tag)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        indexLab.frame = CGRect(x: 10, y: 0, width: 22, height: self.height)
        deleButton.frame = CGRect(x: self.width-40, y: inputField.y, width: 40, height: inputField.height)
        imgView.frame = CGRect(x: deleButton.frame.minX-40, y:10, width: 40, height: self.height-20)
        inputField.frame = CGRect(x: indexLab.frame.maxX, y: 10, width: self.width-indexLab.frame.maxX-deleButton.width-imgView.width-20, height: self.height-20)
        linView.frame = CGRect(x: 0, y: self.height-1, width: self.width, height: 1)
    }
}
